/**
 * 一些demo集合
 * 参考自：
 * https://github.com/OpenFlutter/Flutter-Notebook
 * 
 */

import 'package:flutter/material.dart';
import 'package:fluro/fluro.dart';
import 'package:provider/provider.dart';
import './router/routers.dart';
import './application.dart';
import 'demo/provider_demo/provider/counter_provider.dart';

void main() => runApp(
      Provider<int>.value(
        value: 18,
        child: ChangeNotifierProvider.value(
          value: CounterProvider(),
          child: MyApp(),
        ),
      ),
    );

class MyApp extends StatelessWidget {
  /// 列表item的title
  static const List<String> titleList = [
    "SlideDeleteItemPage",
    // "RightSlideBackPage",
    "ProviderDemoPage",
    "BottomNavigationBarPage",
    "BottomAppBar",
    "RostedGlassPage",
    "AnimaitonPage",
    "SplashDemoListPage"
  ];

  @override
  Widget build(BuildContext context) {
    /// 初始化router
    final router = Router();
    Routers.configureRouters(router);
    Application.router = router;

    /// 上次点击时间
    DateTime _lastPressedAt;
    return MaterialApp(
        onGenerateRoute: Application.router.generator,
        home: WillPopScope(
          /// 双击退出
          onWillPop: () async {
            if (null == _lastPressedAt ||
                DateTime.now().difference(_lastPressedAt) >
                    Duration(seconds: 1)) {
              /// 两次点击间隔超过1秒则重新计时
              _lastPressedAt = DateTime.now();
              return false;
            }
            return true;
          },
          child: Scaffold(
            appBar: AppBar(
              title: Text("起飞"),
            ),
            body: ListView.separated(
              separatorBuilder: (context, index) {
                return Divider(
                  color: Colors.black12,
                  height: 1.0,
                );
              },
              itemCount: titleList.length,
              itemBuilder: (context, index) {
                return ListTile(
                  title: Text(titleList[index]),
                  onTap: () {
                    _onItemClick(context, titleList[index]);
                  },
                );
              },
            ),
          ),
        ));
  }

  void _onItemClick(BuildContext context, String tag) {
    String path = "";
    switch (tag) {
      case "SlideDeleteItemPage":
        path = Routers.slideDeleteItemPage;
        break;

      // case "RightSlideBackPage":
      //   path = Routers.rightSlideBackPage;
      //   break;

      case "ProviderDemoPage":
        path = Routers.providerDemoPage;
        break;

      case "BottomNavigationBarPage":
        path = Routers.bottomNavigationBarPage;
        break;

      case "BottomAppBar":
        path = Routers.bottomAppBarPage;
        break;

      case "RostedGlassPage":
        path = Routers.rostedGlassPage;
        break;

      case "AnimaitonPage":
        path = Routers.animaitonPage;
        break;

      case "SplashDemoListPage":
        path = Routers.splashDemoListPage;
        break;

      default:
    }

    if (path.isNotEmpty) {
      Application.router.navigateTo(context, path);
    }
  }
}
